package edu.hawaii.wattwaiter.page;

import java.util.List;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.ajax.markup.html.IndicatingAjaxLink;
import org.apache.wicket.extensions.ajax.markup.html.tabs.AjaxTabbedPanel;
import org.apache.wicket.extensions.markup.html.tabs.ITab;
import org.apache.wicket.extensions.markup.html.tabs.TabbedPanel;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.panel.Panel;

/**
 * Tabbed Panel for Ajax Navigation.
 * @author Remy
 *
 */
public class MyTabbedPanel extends Panel {

  private static final long serialVersionUID = 1L;

  private final TabbedPanel tabbedPanel;

  /**
   * Constructor for Panel.
   * @param id - id for panel
   * @param tabs - A list of tabs
   */
  public MyTabbedPanel(String id, List<ITab> tabs) {
    super(id);
    tabbedPanel = new AjaxTabbedPanel("mytabpanel", tabs) {
      private static final long serialVersionUID = 1L;

      // Override newLink to supply an IndicatingAjaxLink (wait cursor)

      @Override
      protected WebMarkupContainer newLink(String linkId, final int index) {
        final WebMarkupContainer c = new IndicatingAjaxLink<WebMarkupContainer>(linkId) {
          private static final long serialVersionUID = 1L;

          public void onClick(AjaxRequestTarget target) {
            setSelectedTab(index);
            if (target != null) {
              target.addComponent(MyTabbedPanel.this);
            }
            onAjaxUpdate(target);
          }
        };
        return c;
      }
    };
    tabbedPanel.setOutputMarkupId(true);
    MyTabbedPanel.this.setOutputMarkupId(true);
    add(tabbedPanel);
  }
}
